<template>
  <div class="app-container">

    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="800px" :close-on-click-modal="false">
      <el-form ref="form" :model="form">
        <table align="center" border="0" cellspacing="0" cellpadding="0" class="dialogModule">
          <tr>
            <td class="ContentName">登录账号</td>
            <td class="ContentValue">
              <span class="label">{{form.loginName}}</span>
            </td>
            <td class="ContentName">模块名称</td>
            <td class="ContentValue">
              <span class="label">{{form.moduleName}}</span>
            </td>
          </tr>
          <tr>
            <td class="ContentName">真实姓名</td>
            <td class="ContentValue">
              <span class="label">{{form.createUserName}}</span>
            </td>
            <td class="ContentName">操作时间</td>
            <td class="ContentValue">
              <span class="label">{{form.createTime}}</span>
            </td>
          </tr>

          <tr>
            <td class="ContentName">操作IP</td>

            <td class="ContentValue">
              <span class="label">{{form.operationIp}}</span>
            </td>
            <td class="ContentName">操作状态</td>
            <td class="ContentValue">
              <span class="label">{{form.operationStatusName}}</span>
            </td>
          </tr>
          <tr>
            <td class="ContentName">操作类型</td>

            <td class="ContentValue">
              <span class="label">{{form.operationTypeName}}</span>
            </td>
            <td class="ContentName">请求方法</td>
            <td class="ContentValue">
              <span class="label">{{form.requestMethod}}</span>
            </td>
          </tr>
          <tr>
            <td class="ContentName">请求参数</td>

            <td class="ContentValue">
              <span class="label">{{form.requestParameter}}</span>
            </td>
            <td class="ContentName">请求地址</td>
            <td class="ContentValue">
              <span class="label">{{form.requestUrl}}</span>
            </td>
          </tr>
          <tr v-if="form.returnResult">
            <td class="ContentName">返回结果</td>
            <td colspan="3" class="ContentValueMultiple">  <span class="textarea">{{form.returnResult}}</span> </td>
          </tr>
          <tr v-if="form.exceptionContent">
            <td class="ContentName">异常内容</td>
            <td colspan="3" class="ContentValueMultiple">   <span class="textarea">{{form.exceptionContent}}</span></td>
          </tr>
        </table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="close" @click="cancelAddUpdateOpen">关 闭</el-button>
      </div>
    </el-dialog>

    <el-row :gutter="20">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px" v-show="showSearch" >
          <el-form-item label="登录账号" prop="loginName">
            <el-input v-model="queryParams.loginName" placeholder="请输入登录账号" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="真实姓名" prop="createUserName">
            <el-input v-model="queryParams.createUserName" placeholder="请输入真实姓名" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="模块名称" prop="moduleName">
            <el-input v-model="queryParams.moduleName" placeholder="请输入模块名称" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="操作状态" prop="operationStatusCode">
            <el-select @change="handleQuery" v-model="queryParams.operationStatusCode" size="small" clearable style="width: 150px" placeholder="请选择">
              <el-option key="" label="全部" value=""/>
              <el-option
                v-for="item in operationStatusCodeList"
                :key="item.dictCode"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="操作时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>

          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button  size="mini" :disabled="buttonDisabled(total)" type="primary" icon="el-icon-bottom-right" @click="handleExport('thisPage')" v-hasPermi="['exportSystemLogList']" >导出本页</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button  size="mini" :disabled="buttonDisabled(total)" type="primary" icon="el-icon-download" v-hasPermi="['exportSystemLogList']" @click="handleExport('All')">导出全部</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="querySystemLogPageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table :stripe="true" v-loading="loading" :data="dataList" style="width: 100%" @sort-change='tableSortChange'>
          <el-table-column label="登录账号" align="left" prop="loginName" :show-overflow-tooltip="true"/>
          <el-table-column label="真实姓名" align="left" prop="createUserName"/>
          <el-table-column label="模块名称" align="left" prop="moduleName"/>
          <el-table-column label="操作状态" align="left" prop="operationStatusName" width="150" >
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.operationStatusColor">{{scope.row.operationStatusName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作类型" align="left" prop="operationTypeName" width="150" >
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.operationTypeColor">{{scope.row.operationTypeName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作IP" align="left" prop="operationIp"/>
          <el-table-column label="所属区域" align="left" prop="ipRegion"/>
          <el-table-column label="操作时间" align="center" prop="createTime" sortable='custom' width="200" />
          <el-table-column label="操作"  align="center" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button type="text" size="mini" icon="el-icon-view" @click="handleInfoView(scope.row)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="querySystemLogPageResList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {querySystemLogPageResList, querySystemLogResByLogId, exportSystemLogPageResList} from '@/api/system/systemLog'
  import { getDicListByDictCodeList } from '@/api/common/dict'
  //默认查询参数
  let defaultQueryParams = {
    createUserName: undefined,
    startTime: undefined,
    endTime: undefined,
    loginName: undefined,
    methodName: undefined,
    moduleName: undefined,
    operationIp: undefined,
    operationStatusCode: "",
    operationTypeCode: undefined,
    requestMethod: undefined,
    requestUrl: undefined,
    page: 1,
    rows: 10
  }
  export default {
    name: 'systemLogAdmin',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作日志
        dataList: [],
        // 日期范围
        dateRange: [],
        // 增加修改表单参数
        form: {},
        // 弹出层标题
        title: "",
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        //操作状态
        operationStatusCodeList: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns:[
          { fieldCode: 'loginName' , fieldName: '登录名', visible: true},
          { fieldCode: 'createUserName' , fieldName: '姓名', visible: true},
          { fieldCode: 'moduleName' , fieldName: '模块名称', visible: true},
          { fieldCode: 'operationStatusName' , fieldName: '操作状态', visible: true},
          { fieldCode: 'operationTypeName' , fieldName: '操作类型', visible: true},
          { fieldCode: 'operationIp' , fieldName: '操作IP', visible: true},
          { fieldCode: 'createTime' , fieldName: '操作时间', visible: true}
        ]
      }
    },
    created() {
      this.querySystemLogPageResList()
      this.initDict()
    },
    methods: {
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.querySystemLogPageResList()
      },
      /** 查询日志列表 */
      querySystemLogPageResList() {
        this.loading = true
        querySystemLogPageResList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },
      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = []
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.querySystemLogPageResList()
      },
      //获取数据字典
      initDict(){
        let dictArr = ["OPERATION_STATUS"];
        getDicListByDictCodeList(dictArr).then(res => {
          this.operationStatusCodeList = JSON.parse(JSON.stringify(res.data['OPERATION_STATUS']));
        });
      },
      //查看详情
      handleInfoView(row){
        this.addUpdateOpen = true;
        this.title = "查看详情";
        querySystemLogResByLogId({ logId: row.logId }).then(res => {
          this.form = res.data;
        });
      },
      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },
      /** 导出按钮操作 */
      handleExport(params) {
        //如果是导出本页则 去除分页参数
        if ('All' === params) {
          this.queryParams.rows = 0
        }
        let dataParams = { ...this.queryParams, ...{ excelFieldList: this.columns } }
        this.$confirm('是否确认导出数据?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          exportSystemLogPageResList(dataParams, '操作日志')
        })
      }
    }
  }
</script>
